1
|
|
|
import StorageAdapterInterface from "./adapters/storage-adapter-interface"; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Storage service class. |
5
|
|
|
* Help to read/write/remove tokens as access_token and refresh_token. |
6
|
|
|
*/ |
7
|
|
|
export default class StorageService { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The storage name. Used to prefix localStorage var |
11
|
|
|
*/ |
12
|
|
|
#name: string; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The storage adapter. |
16
|
|
|
*/ |
17
|
|
|
#adapter: StorageAdapterInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Key namespace seprator. |
21
|
|
|
* For example with `#name` 'bedita' the key 'access_token' become `bedita.access_token`. |
22
|
|
|
*/ |
23
|
|
|
#namespaceSeparator = '.'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The access token key |
27
|
|
|
*/ |
28
|
|
|
readonly ACCESS_TOKEN_KEY: string = 'access_token'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The refresh token key |
32
|
|
|
*/ |
33
|
|
|
readonly REFRESH_TOKEN_KEY: string = 'refresh_token'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
* It sets the access token and refresh token keys. |
38
|
|
|
* |
39
|
|
|
* @param name The name used as prefix for store in localStorage |
40
|
|
|
* @param adapter The storage adapter |
41
|
|
|
*/ |
42
|
|
|
public constructor(name = 'bedita', adapter: StorageAdapterInterface) { |
43
|
|
|
this.#name = name; |
44
|
|
|
this.#adapter = adapter; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get access token. |
49
|
|
|
*/ |
50
|
|
|
public getAccessToken(): Promise<string|null> { |
51
|
|
|
return this.get(this.ACCESS_TOKEN_KEY); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set access token. |
56
|
|
|
* |
57
|
|
|
* @param value access token value. |
58
|
|
|
*/ |
59
|
|
|
public setAccessToken(value: string): Promise<any> { |
60
|
|
|
return this.set(this.ACCESS_TOKEN_KEY, value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get refresh token. |
65
|
|
|
*/ |
66
|
|
|
public getRefreshToken(): Promise<string|null> { |
67
|
|
|
return this.get(this.REFRESH_TOKEN_KEY); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set refresh token. |
72
|
|
|
* |
73
|
|
|
* @param value refresh token value. |
74
|
|
|
*/ |
75
|
|
|
public setRefreshToken(value: string): Promise<any> { |
76
|
|
|
return this.set(this.REFRESH_TOKEN_KEY, value); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Remove all tokens. |
81
|
|
|
*/ |
82
|
|
|
public async clearTokens(): Promise<any> { |
83
|
|
|
await this.remove(this.ACCESS_TOKEN_KEY); |
84
|
|
|
await this.remove(this.REFRESH_TOKEN_KEY); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set the namespace separator. |
89
|
|
|
* |
90
|
|
|
* @param separator The namespace separator to use |
91
|
|
|
*/ |
92
|
|
|
public setNamespaceSeparator(separator: string): void |
93
|
|
|
{ |
94
|
|
|
this.#namespaceSeparator = separator; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get namespaced key. |
99
|
|
|
* |
100
|
|
|
* @param key The key. |
101
|
|
|
*/ |
102
|
|
|
protected getNamespacedKey(key: string): string { |
103
|
|
|
return `${this.#name}${this.#namespaceSeparator}${key}`; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the storaged value. |
108
|
|
|
*/ |
109
|
|
|
public get(key: string): Promise<any> { |
110
|
|
|
return this.#adapter.get(this.getNamespacedKey(key)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set a value in the storage. |
115
|
|
|
* |
116
|
|
|
* @param key The starage key |
117
|
|
|
* @param value The value |
118
|
|
|
*/ |
119
|
|
|
public set(key: string, value: any): Promise<any> { |
120
|
|
|
return this.#adapter.set(this.getNamespacedKey(key), value); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Remove a key from the storage. |
125
|
|
|
* |
126
|
|
|
* @param key The key to remove |
127
|
|
|
*/ |
128
|
|
|
public remove(key: string): Promise<any> { |
129
|
|
|
return this.#adapter.remove(this.getNamespacedKey(key)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|